<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;
use Auth;
class Review extends Model
{
    protected $guarded = [];

    public static function normal_comment_pending_number() {
        return self::pending_number(1, function ($dis) {
            if ($dis == null) {
                return false;
            }
            if (strpos($dis->title, '的学习资料') !== false) {
                return false;
            }
            return true;
        });
    }

    public static function study_pending_number() {
        return self::pending_number(1, function ($dis) {
            if ($dis == null) {
                return false;
            }
            if (strpos($dis->title, '的学习资料') === false) {
                return false;
            }
            return true;
        });
    }

    public static function pick_pending_number() {
        $comments = DB::select('select * from comments where jinghua_vote > 0 and `delete` = 0');
        shuffle($comments);
        $reviews = Review::where('reviewer_user_id', Auth::user()->id)->where('type', 3)->get();
        $review_set = array();
        foreach ($reviews as $review) {
            $review_set[$review->comment_id] = true;
        }
        $count = 0;
        foreach ($comments as $comment) {
            if (!array_key_exists($comment->id, $review_set)) {
                $count ++;
            }
        }
        return $count;
    }

    public static function delete_pending_number() {
        $comments = DB::select('select * from comments where delete_vote > 0 and `delete` = 0');
        shuffle($comments);
        $reviews = Review::where('reviewer_user_id', Auth::user()->id)->where('type', 2)->get();
        $review_set = array();
        foreach ($reviews as $review) {
            $review_set[$review->comment_id] = true;
        }
        $count = 0;
        foreach ($comments as $comment) {
            if (!array_key_exists($comment->id, $review_set)) {
                $count ++;
            }
        }
        return $count;
    }

    public static function delete_submitted_number() {
        $comments = DB::select('select * from comments where delete_vote > 0 and `delete` = 0');
        shuffle($comments);
        $reviews = Review::where('reviewer_user_id', Auth::user()->id)->where('type', 2)->get();
        $review_set = array();
        foreach ($reviews as $review) {
            $review_set[$review->comment_id] = true;
        }
        $count = 0;
        foreach ($comments as $comment) {
            if (array_key_exists($comment->id, $review_set)) {
                $count ++;
            }
        }
        return $count;
    }

    public static function pick_submitted_number() {
        $comments = DB::select('select * from comments where jinghua_vote > 0 and `delete` = 0');
        shuffle($comments);
        $reviews = Review::where('reviewer_user_id', Auth::user()->id)->where('type', 3)->get();
        $review_set = array();
        foreach ($reviews as $review) {
            $review_set[$review->comment_id] = true;
        }
        $count = 0;
        foreach ($comments as $comment) {
            if (array_key_exists($comment->id, $review_set)) {
                $count ++;
            }
        }
        return $count;
    }

    public static function pending_number($com_type, $dis_checking) {
        $comments = DB::select('select * from comments where type='. $com_type .' and review=0 and `delete`=0');
        $reviews = DB::select('select * from reviews where type=0 and reviewer_user_id=' . Auth::user()->id);
        $review_set = array();
        foreach ($reviews as $review) {
            $review_set[$review->comment_id] = true;
        }
        $count = 0;
        foreach ($comments as $comment) {
            $dis = Discuss::find($comment->taskid);
            if ($dis_checking($dis) === false) {
                continue;
            }
            if (!array_key_exists($comment->id, $review_set)) {
                $count ++;
                if ($count > 100) {
                    return '100+';
                }
            }
        }
        return $count;
    }
}
